﻿<%@ Page Language="VB"
title="Mindfly Web Studio's Boogaloo - Installation"
validaterequest="false" maintainscrollpositiononpostback="true" %>
<%@ import namespace="Boogaloo.Core.UI" %>
<%@import namespace="Boogaloo.Core.BLL" %>
<%@ import namespace="System.Web.Configuration" %>
<%@ import namespace="System.Data.SqlClient" %>
<%@ import namespace="System.IO" %>
<%@ import namespace="System.Xml" %>
<%@ register tagprefix="boogaloo" assembly="Boogaloo.Admin" namespace="Boogaloo.Admin.UI.Controls" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load      
        If Not Page.IsPostBack And Not Page.IsCallback Then
            txt_connectionstring.Text = GetConnectionString()
            BindPage()
            
        End If
    End Sub
 
    Private Sub BindPage()
        
        cwiz.start_text = "Finish installation"
        cwiz.commit_text = "Commit"
        If rbl_dbtype.SelectedValue = 1 Then
            pnl_custom.Visible = False
            lit_commit_headline.Text = "Finish your installation"
            lit_commit_message.Visible = False
            cwiz.message = "Reminder: Remove the <em>/Install</em> directory once installation is complete."
        Else
            pnl_custom.Visible = True
            lit_commit_message.Visible = True
            lit_commit_headline.Text = "Execute the Database Install Scripts"
            lit_commit_message.Text = "<p>In the install directory, you will find two "".sql"" files.</p><ol><li>boogaloo_install.sql</li><li>default_values.sql</li></ol><p>Please execute these scripts in the database you intend to you.</p>"
            cwiz.message = "The next step requires that the database scripts have been executed.  Please verify you are ready to proceed."
        End If
        
        If show_execute Then
            lb_install.Visible = False
            pnl.Visible = True
        Else
            lb_install.Visible = False
            pnl.Visible = False
        End If
        
    End Sub
    
    Protected Sub lb_build_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lb_build.Click
        SetConnectionString()
    End Sub
        
    Protected Sub lb_save_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lb_save.Click        
        If Page.IsValid Then
            SaveConnectionString()
            show_execute = True
            BindPage()
        End If        
    End Sub
    
    
    Private Function GetConnectionString() As String
        Dim doc As XmlDocument = GetConfigDoc()
        Dim el As XmlElement = CType(doc.DocumentElement.SelectSingleNode("add[@name='BoogalooSqlServer']"), XmlElement)
        Return el.GetAttribute("connectionString")
    End Function
    
    Private Sub SaveConnectionString()
        Dim doc As XmlDocument = GetConfigDoc()
        Dim el As XmlElement = CType(doc.DocumentElement.SelectSingleNode("add[@name='BoogalooSqlServer']"), XmlElement)
        el.SetAttribute("connectionString", txt_connectionstring.Text)
        doc.Save(config_path)                        
    End Sub
    
    Private Sub cwiz_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cwiz.Click
        Response.Redirect("Installer.aspx")
    End Sub
    
    Private Function GetConfigDoc() As XmlDocument
        Dim doc As New XmlDocument                    
        doc.Load(config_path)
        Return doc
    End Function
    
    Private Sub rbl_dbtype_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rbl_dbtype.SelectedIndexChanged
        show_execute = False
        BindPage()
    End Sub
    
    Private Sub SetConnectionString()
        txt_connectionstring.Text = String.Format("Data Source={0},1433;Network Library=DBMSSOCN;Initial Catalog={1};User ID={2};Password={3}", txt_datasource.Text, txt_catalog.Text, txt_username.Text, txt_password.Text)
    End Sub
    
    
    Private _show_execute As Boolean = False
    Public Property show_execute() As Boolean
        Get
            Return _show_execute
        End Get
        Set(ByVal value As Boolean)
            _show_execute = value
        End Set
    End Property

    Private config_path As String = Server.MapPath("~/App_Data/Database.config")
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Boogaloo Installation</title>
    <link rel="stylesheet" type="text/css" runat="server" href="~/App_Themes/boogaloo/a_boogaloo_reset.css" />
    <link rel="stylesheet" type="text/css" runat="server" href="~/App_Themes/boogaloo/b_boogaloo_extensions.css" />
    <link rel="stylesheet" type="text/css" runat="server" href="~/App_Themes/boogaloo/c_layout.css" />
    <link rel="stylesheet" type="text/css" runat="server" href="~/App_Themes/boogaloo/d_type.css" />
    <link rel="stylesheet" type="text/css" runat="server" href="~/App_Themes/boogaloo/e_color.css" />
    <link rel="stylesheet" type="text/css" runat="server" href="~/App_Themes/boogaloo/f_extensions.css" />
    <link rel="stylesheet" type="text/css" runat="server" href="~/App_Themes/boogaloo/g_print.css" media="print" />
    <link rel="stylesheet" type="text/css" runat="server" href="~/App_Themes/boogaloo/h_mobile.css" media="mobile" />
    <!--[if lt IE 8]>
    <link rel="stylesheet" type="text/css" runat="server" href="~/App_Themes/boogaloo/i_ie.css" />
    <![endif]-->
    <!--[if lt IE 7]>
    <link rel="stylesheet" type="text/css" runat="server" href="~/App_Themes/boogaloo/j_ie6.css" />
    <![endif]-->

</head>
<body>
    <form id="form1" runat="server">
    <asp:scriptmanager id="sm" runat="server" />
    <div id="header">
        <h1><a title="Boogaloo">Boogaloo</a> <span>The <em>groovy</em> way to manage your site.</span></h1>        
    </div>
    
    <div id="content"><div class="section main">
            <h2>Installation - Step 2</h2>
            


            <fieldset class="box enriched active">
            <h3>Database Connection String</h3>
            
            
            <div class="pseudolabel">
                <asp:radiobuttonlist id="rbl_dbtype" runat="server" autopostback="true">
                    <asp:listitem value="1" selected="True">SQL Express 2008</asp:listitem>
                    <asp:listitem value="2">SQL Server (Custom)</asp:listitem>
                </asp:radiobuttonlist>
            </div>
            
            <asp:panel id="pnl_custom" runat="server">
            
            <asp:label id="lbl_txt_datasource" runat="server" associatedcontrolid="txt_datasource">
                <span>Data Source (database server IP or name)</span> <asp:requiredfieldvalidator controltovalidate="txt_datasource" id="rfv_txt_datasource" runat="server" display="Dynamic" errormessage=" * required" cssclass="error" />
                <asp:textbox id="txt_datasource" runat="server" text="[server name]" cssclass="medium" />
            </asp:label>
            
            <asp:label id="lbl_txt_catalog" runat="server" associatedcontrolid="txt_catalog">
                <span>Catalog (database name)</span> <asp:requiredfieldvalidator controltovalidate="txt_catalog" id="rfv_txt_catalog" runat="server" display="Dynamic" errormessage=" * required" cssclass="error" />
                <asp:textbox id="txt_catalog" runat="server" cssclass="short" text="boogaloo" />
            </asp:label>
            
            <asp:label id="lbl_txt_username" runat="server" associatedcontrolid="txt_username">
                <span>Username (dbo user)</span> <asp:requiredfieldvalidator controltovalidate="txt_username" id="rfv_txt_username" runat="server" display="Dynamic" errormessage=" * required" cssclass="error" />
                <asp:textbox id="txt_username" runat="server" text="sa" cssclass="short" />
            </asp:label>
            
            
            <asp:label id="lbl_txt_password" runat="server" associatedcontrolid="txt_password">
                <span>Password</span> <asp:requiredfieldvalidator controltovalidate="txt_password" id="rfv_txt_password" runat="server" display="Dynamic" errormessage=" * required" cssclass="error" />
                <asp:textbox id="txt_password" runat="server" text="[password]" cssclass="short" />
            </asp:label>
            
            <asp:linkbutton id="lb_build" runat="server" cssclass="button" text="Create Connection String" />
            
            </asp:panel>
            

            
            
            <asp:label id="lbl_txt_connectionstring" runat="server" associatedcontrolid="txt_connectionstring">
                <span>Connection string</span> <asp:requiredfieldvalidator controltovalidate="txt_connectionstring" id="rfv_txt_connectionstring" runat="server" display="Dynamic" errormessage=" * required" cssclass="error" validationgroup="connectionstring" />
                <asp:textbox id="txt_connectionstring" runat="server" cssclass="longest" />
            </asp:label>
                       
            
            
            <asp:linkbutton id="lb_save" runat="server" cssclass="button" text="Use this connection string" validationgroup="connectionstring" />
            
                              
            </fieldset>


            
        </div><div class="aside settings">
            <asp:panel id="pnl" runat="server">
            <h3><asp:literal id="lit_commit_headline" runat="server"></asp:literal></h3>
            <asp:literal id="lit_commit_message" runat="server" />          
            <asp:updatepanel id="up" runat="server">
            <contenttemplate>
            <boogaloo:commitwizard id="cwiz" runat="server" cssclass="button" message="Thank you for trying Boogaloo!<br />" />
            </contenttemplate>
            </asp:updatepanel>
            
            <asp:linkbutton id="lb_install" runat="server" cssclass="button" text="Execute Database Scripts" validationgroup="connectionstring" />
            </asp:panel>
    </div></div>        
    
    </form>
</body>
</html>
